/*
 * Blurse.java
 *
 * Created on January 5, 2008, 11:48 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class Blurse {
     private dbConnection dbcon = new dbConnection();
     private String characterName, godname;
     private int turn, battle;
    /** Creates a new instance of Blurse */
    public Blurse() {
    }
    public Blurse (String cname, String gname, int b, int t) {
        setCharacterName(cname);
        setGodname(gname);
        turn = t;
        battle = b;
   }
     public boolean checkExist(Blurse b)
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM Blurse WHERE characterName = ? AND godname = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, b.getCharacterName());
            stmt.setString(2, b.getGodname());
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
   public void addBlurse (Blurse b){
      						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        if (!checkExist(b))
        {
            getDbcon().connect();
            query = "INSERT INTO Blurse (characterName, godname, turn, battle) VALUES (?,?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, b.getCharacterName());
                stmt.setString(2, b.getGodname());
                stmt.setInt(3, b.getTurn());
                stmt.setInt(4, b.getBattle());
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            getDbcon().disconnect();
        }
    }
    public Vector viewBlurse (String cName){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Blurse> bList;
            
        bList = new Vector<Blurse>();
        Iterator it = bList.iterator();
                
        getDbcon().connect();
        query = "SELECT characterName, godname, turn, battle FROM Blurse WHERE characterName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cName);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Blurse b = new Blurse();
                b.setCharacterName(rs.getString("characterName").trim());
                b.setGodname(rs.getString("godname").trim());
                b.turn = rs.getInt("turn");
                b.battle = rs.getInt("battle");
                bList.add(b);
              
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return bList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
     public void deleteBlurse (String cname, String bname){
         		
    	String query;		
    	ResultSet rs;   
        PreparedStatement stmt;
        
       	getDbcon().connect();						
        query = "DELETE FROM Blurse WHERE characterName = ? AND godname = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);	
            stmt.setString(1, cname);
            stmt.setString(2, bname);
            stmt.executeUpdate();				
            stmt.close();						
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
     }
     public void deleteBlurse (Blurse b){
         		
    	String query;		
    	ResultSet rs;   
        PreparedStatement stmt;
        
       	getDbcon().connect();						
        query = "DELETE FROM Blurse WHERE characterName = ? AND godname = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);	
            stmt.setString(1, b.getCharacterName());
            stmt.setString(2, b.getGodname());
            stmt.executeUpdate();				
            stmt.close();						
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
     }
  public void editBlurse (String characters, String sname, int bat, int tur){
        
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;
        getDbcon().connect();
        query = "UPDATE Blurse SET turn = ?, battle = ? WHERE characterName = ? AND godname = ? ";
        try 
        {
             
            stmt = getDbcon().con.prepareStatement(query); 
            stmt.setInt (1, tur);
            stmt.setInt (2, bat);
            stmt.setString(3, characters);
            stmt.setString(4, sname);
            stmt.executeUpdate();	
            stmt.close();					
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
    }
    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getCharacterName() {
        return characterName;
    }

    public void setCharacterName(String characterName) {
        this.characterName = characterName;
    }

    public String getGodname() {
        return godname;
    }

    public void setGodname(String godname) {
        this.godname = godname;
    }
    
     public int getTurn() {
        return turn;
    }

    public void setTurn(int turn) {
        this.turn = turn;
    }
      public int getBattle() {
        return battle;
    }

    public void setBattle(int battle) {
        this.battle = battle;
    }
}
